package com.example.blog.admin.controller;

import com.example.blog.common.aop.LogAnnotation;
import com.example.blog.common.cache.Cache;
import com.example.blog.admin.service.ArticleService;
import com.example.blog.admin.vo.ArticleVo;
import com.example.blog.admin.vo.Result;
import com.example.blog.admin.vo.param.ArticleParam;
import com.example.blog.admin.vo.param.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // Result 是统一的返回结果

    // /articles/publish
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        return articleService.insert(articleParam);
    }

    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    @PostMapping
    @LogAnnotation(module = "文章", operation = "获取文章列表")
    @Cache(expire = 5*60*1000, name = "articles")
    public Result articles(@RequestBody PageParams pageParams) {

//        int I = 10/0;

        // ArticleVo 是页面接收的数据
        List<ArticleVo> articleVoList = articleService.listArticlesPage(pageParams);
        return Result.success(articleVoList);
    }

    /**
     *  首页 最热文章
     * @return
     */
    @PostMapping("hot")
    @Cache(expire = 5*60*1000, name = "hot_articles")
    public Result hotArticles() {
        int limit = 3;
        return Result.success(articleService.findHotArticles(limit));
    }

    /**
     *
     * @return
     */
    @PostMapping("new")
    @Cache(expire = 5*60*1000, name = "new_articles")
    public Result newArticles() {
        int limit = 3;
        return Result.success(articleService.findNewArticles(limit));
    }

    /**
     * 首页 文章归档
     * @return
     */
    @PostMapping("listArchives")
    public Result listArchives() {
        return Result.success(articleService.listArchives());
    }

    /**
     * w文章详情
     * @param articleId
     * @return
     */
    @PostMapping("view/{id}")
    public Result articleDetail(@PathVariable("id") Long articleId) {
        return Result.success(articleService.findArticleById(articleId));
    }
}
